﻿Public Class FViewSharedItems
    Private Shared mActiveForms As New Generic.Dictionary(Of Integer, FViewSharedItems)

    Public Shared Shadows Sub Show(ByVal owner As IWin32Window, ByVal share As Entities.Share)
        If mActiveForms.ContainsKey(share.Id) Then
            mActiveForms(share.Id).Activate()
        Else
            Dim clsView As New FViewSharedItems
            clsView.ShowIt(share)
            mActiveForms.Add(share.Id, clsView)
        End If
    End Sub

    Public Shared Sub CloseAll()
        For i As Integer = mActiveForms.Count - 1 To 0 Step -1
            mActiveForms.Values(i).Close()
        Next
    End Sub

    Private mShare As Entities.Share

    Private Sub ShowIt(ByVal share As Entities.Share)
        mShare = share

        Me.Text = String.Format("{0} - Shared Items - Buddy 2 Buddy", share.Name)
        Me.Icon = Framework.My.Resources.Share

        Dim aItems As Generic.List(Of Entities.SharedItem)
        aItems = EntityHandlers.SharedItems.GetAllByShareId(share.Id)

        ListView1.BeginUpdate()
        For Each clsItem As Entities.SharedItem In aItems
            UpdateItem(clsItem, True)
        Next
        ListView1.EndUpdate()
        MyBase.Show()
    End Sub

    Private Sub RemoveItem(ByVal item As Entities.SharedItem)
        For Each clsItem As ListViewItem In ListView1.Items
            If CType(clsItem.Tag, Entities.SharedItem).Id = item.Id Then
                clsItem.Remove()
                Exit For
            End If
        Next
    End Sub
    Private Sub UpdateItem(ByVal item As Entities.SharedItem, ByVal fromLoad As Boolean)
        Dim clsItem As ListViewItem = Nothing

        If Not fromLoad Then
            For Each clsTmpItem As ListViewItem In ListView1.Items
                If CType(clsTmpItem.Tag, Entities.SharedItem).Id = item.Id Then
                    clsItem = clsTmpItem
                    Exit For
                End If
            Next
        End If
        If clsItem Is Nothing Then
            clsItem = ListView1.Items.Add(String.Empty)
            For i As Integer = 1 To 4 : clsItem.SubItems.Add(String.Empty) : Next
        End If

        Dim strImageKey As String = item.ItemType.ToString
        If Not ImageList1.Images.ContainsKey(strImageKey) Then
            Select Case item.ItemType
                Case Entities.ItemType.Movies
                    ImageList1.Images.Add(item.ItemType.ToString, My.Resources.Movie)
                Case Else
                    strImageKey = Nothing
            End Select
        End If
        With clsItem
            .ImageKey = strImageKey
            .SubItems(0).Text = item.Name
            .SubItems(1).Text = item.Added.ToLocalTime.ToString("g")
            .SubItems(2).Text = item.LastChanged.ToLocalTime.ToString("g")
            .SubItems(3).Text = item.TotalFiles.ToString
            .SubItems(4).Text = Framework.Misc.ToReadableFileSize(item.TotalSize)
            .Tag = item
        End With
    End Sub

    Private Sub FViewSharedItems_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mActiveForms.Remove(mShare.Id)
        SetupEvents(False)
    End Sub

    Private Sub FViewSharedItems_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetupEvents(True)
    End Sub

    Private Sub SetupEvents(ByVal startListening As Boolean)
        If startListening Then
            AddHandler EntityHandlers.SharedItems.DeletedGUI, AddressOf EventHandler_SharedItemDeleted
            AddHandler EntityHandlers.SharedItems.UpdatedGUI, AddressOf EventHandler_SharedItemUpdated
        Else
            RemoveHandler EntityHandlers.SharedItems.DeletedGUI, AddressOf EventHandler_SharedItemDeleted
            RemoveHandler EntityHandlers.SharedItems.UpdatedGUI, AddressOf EventHandler_SharedItemUpdated
        End If
    End Sub

    Private Sub EventHandler_SharedItemDeleted(ByVal item As Entities.SharedItem)
        Try
            If mShare.Id = item.ShareId Then RemoveItem(item)
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub
    Private Sub EventHandler_SharedItemUpdated(ByVal item As Entities.SharedItem)
        Try
            If mShare.Id = item.ShareId Then UpdateItem(item, False)
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub ListView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView1.KeyDown
        Try
            If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then
                e.Handled = True
                EventHandler_ShowSelectedItem()
            End If
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        EventHandler_ShowSelectedItem()
    End Sub

    Private Function GetSelectedItem() As Entities.SharedItem
        If ListView1.SelectedItems.Count <> 1 Then Return Nothing
        Return CType(ListView1.SelectedItems(0).Tag, Entities.SharedItem)
    End Function


    Private Sub EventHandler_ShowSelectedItem()
        Try
            Dim clsItem As Entities.SharedItem = GetSelectedItem()
            If clsItem IsNot Nothing Then FViewSharedItemDetails.Show(Me, clsItem)
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub GenerateOpenMediaInfoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateOpenMediaInfoToolStripMenuItem.Click
        Dim clsItem As Entities.SharedItem = GetSelectedItem()
        If clsItem IsNot Nothing Then FileShare.UI.Manager.ModifyOpenMediaInfo(clsItem)
    End Sub

    Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
        If ListView1.SelectedItems.Count <> 1 Then
            GenerateOpenMediaInfoToolStripMenuItem.Enabled = False
        Else
            Dim clsItem As Entities.SharedItem = CType(ListView1.SelectedItems(0).Tag, Entities.SharedItem)
            Select Case clsItem.ItemType
                Case Entities.ItemType.Movies, Entities.ItemType.TVSeries
                    GenerateOpenMediaInfoToolStripMenuItem.Enabled = True
                Case Else
                    GenerateOpenMediaInfoToolStripMenuItem.Enabled = False
            End Select
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub
End Class